﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace uniview
{
    public class TrafficFlow
    {
        public void SetData(byte[] data)
        {

            string xmlstr = Encoding.Default.GetString(data);


            XDocument doo = XDocument.Parse(xmlstr);

            var s = doo.Root;
            Lane = new List<LaneItem>();

            foreach (var item in s.Elements())
            {
                switch (item.Name.ToString())
                {
                    // 节点
                    case "CamID":
                        CamID = item.Value.ToString();
                        break;
                    case "SampleTime":
                        try
                        {
                            SampleTime = DateTime.ParseExact(item.Value.ToString().Trim(), "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.CurrentCulture);
                        }
                        catch
                        {
                            SampleTime = DateTime.ParseExact("20000101000000", "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
                        }
                        break;
                    case "Period":
                        Period = Convert.ToInt32(item.Value.ToString());
                        break;
                    case "TollgateID":
                        TollgateID = item.Value.ToString();
                        break;
                    case "LaneNum":
                        LaneNum = Convert.ToInt32(item.Value.ToString());
                        break;
                    case "Lane":
                        LaneItem laneitem = new LaneItem();
                        foreach (var laneinfo in item.Elements())
                        {
                            // LANE信息
                            switch (laneinfo.Name.ToString())
                            {
                                case "LaneID":
                                    laneitem.LaneID = laneinfo.Value.ToString();
                                    break;
                                case "State":
                                    laneitem.State = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "Flow":
                                    laneitem.Flow = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "Direction":
                                    laneitem.Direction = laneinfo.Value.ToString();
                                    break;
                                case "DirectionName":
                                    laneitem.DirectionName = laneinfo.Value.ToString();
                                    break;
                                case "Vehicles":
                                    laneitem.Vehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "AverageSpeed":
                                    laneitem.AverageSpeed = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "VehicleLength":
                                    laneitem.VehicleLength = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "TimeOccupyRatio":
                                    laneitem.TimeOccupyRatio = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "SpaceOccupyRatio":
                                    laneitem.SpaceOccupyRatio = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "SpaceHeadway":
                                    laneitem.SpaceHeadway = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "TimeHeadway":
                                    laneitem.TimeHeadway = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "Density":
                                    laneitem.Density = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "OverSpeedVehicles":
                                    laneitem.OverSpeedVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "UnderSpeedVehicles":
                                    laneitem.UnderSpeedVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "LargeVehicles":
                                    laneitem.LargeVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "MediumVehicles":
                                    laneitem.MediumVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "SmallVehicles":
                                    laneitem.SmallVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "MotoVehicles":
                                    laneitem.MotoVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "SedanVehicles":
                                    laneitem.SedanVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "SUVVehicles":
                                    laneitem.SUVVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "MinibusVehicles":
                                    laneitem.MinibusVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "MinivanVehicles":
                                    laneitem.MinivanVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "MediumBusVehicles":
                                    laneitem.MediumBusVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "LargeBusVehicles":
                                    laneitem.LargeBusVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "LargeVanVehicles":
                                    laneitem.LargeVanVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "LongVehicles":
                                    laneitem.LongVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "WhitePlateVehicles":
                                    laneitem.WhitePlateVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "YellowPlateVehicles":
                                    laneitem.YellowPlateVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "BluePlateVehicles":
                                    laneitem.BluePlateVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "BlackPlateVehicles":
                                    laneitem.BlackPlateVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "OtherPlateVehicles":
                                    laneitem.OtherPlateVehicles = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "Volume":
                                    laneitem.Volume = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "FlowRate":
                                    laneitem.FlowRate = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "TravelTime":
                                    laneitem.TravelTime = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                case "Delay":
                                    laneitem.Delay = Convert.ToInt32(laneinfo.Value.ToString());
                                    break;
                                default:
                                    break;
                            }
                        }
                        Lane.Add(laneitem);
                        break;

                    default:
                        break;
                }
            }

        }

        /// <summary>
        /// 卡口相机编号
        /// </summary>
        public string CamID { get; set; }
        /// <summary>
        /// 采样时间
        /// </summary>
        public DateTime SampleTime { get; set; }
        /// <summary>
        /// 统计周期
        /// </summary>
        public int Period { get; set; }
        /// <summary>
        /// 卡口编码
        /// </summary>
        public string TollgateID { get; set; }
        /// <summary>
        /// 车道总数量
        /// </summary>
        public int LaneNum { get; set; }

        public List<LaneItem> Lane { get; set; }
    }

    public class LaneItem
    {

        /// <summary>
        /// 车道编号
        /// </summary>
        public string LaneID { get; set; }
        /// <summary>
        /// 流量状态
        /// </summary>
        public int State { get; set; }
        /// <summary>
        /// 流量值
        /// </summary>
        public int Flow { get; set; }
        /// <summary>
        /// 方向编号
        /// </summary>
        public string Direction { get; set; }
        /// <summary>
        /// 方向名称
        /// </summary>
        public string DirectionName { get; set; }
        /// <summary>
        /// 通过车辆总数
        /// </summary>
        public int Vehicles { get; set; }
        /// <summary>
        /// 平均车速
        /// </summary>
        public int AverageSpeed { get; set; }
        /// <summary>
        /// 车外廓长
        /// </summary>
        public int VehicleLength { get; set; }
        /// <summary>
        /// 时间占有率
        /// </summary>
        public int TimeOccupyRatio { get; set; }
        /// <summary>
        /// 空间占有率
        /// </summary>
        public int SpaceOccupyRatio { get; set; }
        /// <summary>
        /// 车头间距
        /// </summary>
        public int SpaceHeadway { get; set; }
        /// <summary>
        /// 车头时距
        /// </summary>
        public int TimeHeadway { get; set; }
        /// <summary>
        /// 车辆密度
        /// </summary>
        public int Density { get; set; }
        /// <summary>
        /// 超速车辆数
        /// </summary>
        public int OverSpeedVehicles { get; set; }
        /// <summary>
        /// 低速车辆数
        /// </summary>
        public int UnderSpeedVehicles { get; set; }
        /// <summary>
        /// 大型车交通量 
        /// </summary>
        public int LargeVehicles { get; set; }
        /// <summary>
        /// 中型车交通量
        /// </summary>
        public int MediumVehicles { get; set; }
        /// <summary>
        /// 小型车交通量
        /// </summary>
        public int SmallVehicles { get; set; }
        /// <summary>
        /// 摩托车交通量
        /// </summary>
        public int MotoVehicles { get; set; }
        /// <summary>
        /// 超长车交通量 
        /// </summary>
        public int LongVehicles { get; set; }
        /// <summary>
        /// 轿车交通量
        /// </summary>
        public int SedanVehicles { get; set; }
        /// <summary>
        /// SUV交通量
        /// </summary>
        public int SUVVehicles { get; set; }
        /// <summary>
        /// 面包车交通量
        /// </summary>
        public int MinibusVehicles { get; set; }
        /// <summary>
        /// 小货车交通量
        /// </summary>
        public int MinivanVehicles { get; set; }
        /// <summary>
        /// 中巴车交通量
        /// </summary>
        public int MediumBusVehicles { get; set; }
        /// <summary>
        /// 大客车交通量
        /// </summary>
        public int LargeBusVehicles { get; set; }
        /// <summary>
        /// 大货车交通量
        /// </summary>
        public int LargeVanVehicles { get; set; }
        /// <summary>
        /// 白牌车车流量
        /// </summary>
        public int WhitePlateVehicles { get; set; }
        /// <summary>
        /// 黄牌车车流量
        /// </summary>
        public int YellowPlateVehicles { get; set; }
        /// <summary>
        /// 蓝牌车车流量
        /// </summary>
        public int BluePlateVehicles { get; set; }
        /// <summary>
        /// 黑牌车车流量
        /// </summary>
        public int BlackPlateVehicles { get; set; }
        /// <summary>
        /// 其它牌车流量
        /// </summary>
        public int OtherPlateVehicles { get; set; }
        /// <summary>
        /// 交通量
        /// </summary>
        public int Volume { get; set; }
        /// <summary>
        /// 流率
        /// </summary>
        public int FlowRate { get; set; }
        /// <summary>
        /// 排队长度
        /// </summary>
        public int BackOfQueue { get; set; }
        /// <summary>
        /// 旅行时间
        /// </summary>
        public int TravelTime { get; set; }
        /// <summary>
        /// 延误
        /// </summary>
        public int Delay { get; set; }
    }
}
